草庐IT

c++ - C++中static的含义

全部标签

c++ - 具有多个参数的 boost::static_visitor

typedefboost::variantType;classAppend:publicboost::static_visitor{public:voidoperator()(int){}voidoperator()(double){}};Typetype(1.2);Visitorvisitor;boost::apply_visitor(visitor,type);是否可以更改访问者,使其接收如下额外数据:classAppend:publicboost::static_visitor{public:voidoperator()(int,conststd::string&){}voido

c++ - 是否有可能 static_assert lambda 不是通用的?

我实现了一个Visit函数(在变体上),它检查变体中当前事件的类型是否与函数签名(更准确地说是第一个参数)匹配。基于这个不错answer.例如#include#include#includetemplateArgfirst_argument_helper(Ret(*)(Arg,Rest...));templateArgfirst_argument_helper(Ret(F::*)(Arg,Rest...));templateArgfirst_argument_helper(Ret(F::*)(Arg,Rest...)const);templatedecltype(first_argum

c++ - static_cast 可以在 C++ 中抛出异常吗?

假设static_cast永远不会抛出异常是否安全?对于int到Enum的转换,即使无效也不会抛出异常。我可以依赖这种行为吗?以下代码有效。enumanimal{CAT=1,DOG=2};inty=10;animalx=static_cast(y); 最佳答案 对于这种特定类型的转换(枚举类型的组成部分),可能会抛出异常。C++standard5.2.9Staticcast[expr.static.cast]paragraph7Avalueofintegralorenumerationtypecanbeexplicitlyconve

c++ - 在结构/类中使用 static const int

structA{staticconstinta=5;structB{staticconstintb=a;};};intmain(){returnA::B::b;}上面的代码可以编译。但是,如果您阅读ScottMyers的《EffectiveC++》一书(第14页);除了声明之外,我们还需要a的定义。谁能解释为什么这是一个异常(exception)? 最佳答案 C++编译器允许staticconst整数(并且仅限于整数)在它们声明的位置指定它们的值。这是因为基本上不需要该变量,它只存在于代码中(通常是编译出来的)。其他变量类型(例如s

c++ - 在编译时在 static_assert() 中显示整数

这是我正在尝试做的简化版本enumFirst{a,b,c,nbElementFirstEnum,};enumSecond{a,b,c,nbElementSecondEnum,};static_assert(First::nbElementFirstEnum==Second::nbElementSecondEnum,"Notthesamenumberofelementintheenums.");/*static_assert(First::nbElementFirstEnum==Second::nbElementSecondEnum,"Notthesamenumberofelementi

c++ - C++ 标准甚至定义了 "lock-free"的含义吗?

我找不到基于锁和无锁原子之间的语义差异。据我所知,就语言而言,差异在语义上没有意义,因为该语言不提供任何时间保证。我能找到的唯一保证是内存排序保证,这两种情况似乎都相同。(如何)原子的无锁性会影响程序语义?即,除了调用is_lock_free或atomic_is_lock_free之外,是否有可能编写一个定义明确的程序,其行为实际上受到原子是否无锁的影响?这些功能甚至具有语义意义吗?或者它们只是用于编写响应式程序的实用hack,即使该语言从一开始就没有提供时间保证? 最佳答案 至少有一个语义差异。根据C++111.9程序执行/6:W

c++ - const_cast 与 static_cast

添加const到非常量对象,哪个是首选方法?const_cast或static_cast.在最近的一个问题中,有人提到他们更喜欢使用static_cast,但我会认为const_cast将使代码的意图更加清晰。那么使用static_cast的理由是什么?使变量成为常量? 最佳答案 也不要使用。初始化引用对象的const引用:Tx;constT&xref(x);x.f();//callsnon-constoverloadxref.f();//callsconstoverload或者,使用implicit_cast函数模板,例如theo

c++ - 对 `Static Class Member variable inside Static member function' 的 undefined reference

我实际上正在尝试实现分页的模拟,在我的内存管理器中,我尝试创建一个静态页表,但是当我尝试打印它时它给出了引用错误。#ifndefMEMORYMANAGER_H#defineMEMORYMANAGER_H#include"memory.h"classMemoryManager{private:PhysicalMemoryRAM;LogicalMemoryVM;intoffsetValue;staticint**pageTable;public:MemoryManager();booladdProcess(TimeSliceRequest);voidprintVirtualMemory()

c++ - 为什么全局 const char 需要 "static"而 bool 不需要?

共享header。我能做到:constboolkActivatePlayground=false;包含在多个文件中时工作正常。我不能这样做:constchar*kActivePlayground="kiddiePool";导致错误:重复的符号。但这行得通:staticconstchar*kActivePlayground="kiddiePool";为什么constchar*需要static而constbool不需要?另外,我认为static不是必需的,因为const总是static隐式? 最佳答案 在C++中,const变量默认有静

c++ - static_cast 安全

AFAIK,对于指针/引用static_cast,如果此时编译器看不到类定义,则static_cast的行为将类似于reinterpret_cast。为什么static_cast对指针/引用不安全而对数值安全? 最佳答案 简而言之,因为多重继承。长:#includestructA{inta;};structB{intb;};structC:A,B{intc;};intmain(){Cc;std::cout(&c)(&c)输出:Cisat:0x22ccd0Bisat:0x22ccd4Aisat:0x22ccd0请注意,为了正确转换为B